package cn.cheqi.recovery.api.interceptor;

import cn.hutool.json.JSONObject;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;

/**
 * 访问与api路径相关的接口
 */

@Component
public class ApiInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String uri = request.getRequestURI();
        HttpSession session = request.getSession();
        /*String sessionCheckCode = (String) session.getAttribute("checkCode");
        String inputCheckCode = request.getParameter("checkCode");*/
        //判断session中的验证码是否为空
        /*if (sessionCheckCode == null){
            //重置response
            response.reset();
            //设置编码格式
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json;charset=UTF-8");
            PrintWriter pw = response.getWriter();
            JSONObject res = new JSONObject();
            res.put("code",1);
            res.put("msg","您还未登录,请先登录");
            res.put("data","");
            pw.append(res.toString());
            return false;
        }
        //判断输入的验证码是否为空
        if (inputCheckCode == null){
            //重置response
            response.reset();
            //设置编码格式
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json;charset=UTF-8");
            PrintWriter pw = response.getWriter();
            JSONObject res = new JSONObject();
            res.put("code",1);
            res.put("msg","请先输入验证码");
            res.put("data","");
            pw.append(res.toString());
            return false;
        }
        //如果session中的验证码以及输入的验证码都不为空
        if (uri.startsWith("/api") && !sessionCheckCode.equals(inputCheckCode.replace(" ","+"))){
            //重置response
            response.reset();
            //设置编码格式
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json;charset=UTF-8");
            PrintWriter pw = response.getWriter();
            JSONObject res = new JSONObject();
            res.put("code",1);
            res.put("msg","验证码错误");
            res.put("data","");
            pw.append(res.toString());
            return false;
        }*/
        String sessionId =  (String) session.getAttribute("sessionId");
        //String inSessionId = session.getId();
        //String inputSessionId = request.getParameter("sessionId");
        //判断是否已经登录
        if (sessionId == null){
            //重置response
            response.reset();
            //设置编码格式
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json;charset=UTF-8");
            PrintWriter pw = response.getWriter();
            JSONObject res = new JSONObject();
            res.put("code",1);
            res.put("msg","您还未登录,请先登录");
            res.put("data","");
            pw.append(res.toString());
            return false;
        }
        /*//判断是否携带sessionId
        if (inputSessionId == null){
            //重置response
            response.reset();
            //设置编码格式
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json;charset=UTF-8");
            PrintWriter pw = response.getWriter();
            JSONObject res = new JSONObject();
            res.put("code",1);
            res.put("msg","sessionId为空");
            res.put("data","");
            pw.append(res.toString());
            return false;
        }*/
        //判断是否为同一次会话
        /*if (uri.startsWith("/api") && !sessionId.equals(inputSessionId)){
            //重置response
            response.reset();
            //设置编码格式
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json;charset=UTF-8");
            PrintWriter pw = response.getWriter();
            JSONObject res = new JSONObject();
            res.put("code",1);
            res.put("msg","不是同一次会话");
            res.put("data","");
            pw.append(res.toString());
            return false;
        }*/
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
